1 from utils import (
2 safe_input,
3 select_by_id_or_name,
4 today,
5 get_products,
6 update_products,
7 get_employees,
8 get_sales,
9 add_sale
10 )
11
12
13 def register_sale():
14 sale = {
15 "date": today()
16 }
17
18 print("--- Register sale ---\n")
19 print("Who is selling the product?")
20
21 employees = get_employees()
22 for e in employees:
23 print("- %s (%s)" % (e['name'], e['id']))
24 print()
25
26 employee = select_by_id_or_name(employees, 'employee')
27 sale['employee_id'] = employee['id']
28
29 print("Selected: %s (%s)\n" % (employee['name'], employee['id']))
30 print("Which product is it?")
31 products = get_products()
32
33 for p in products:
34 print("- %s (%s) (%s in stock)" %
35 (p['name'], p['id'], p['quantity']))
36 print()
37
38 product = select_by_id_or_name(products, 'product')
39
40 sale['product_id'] = product['id']
41
42 print("Selected: %s (%s) (%s in stock)\n" %
43 (product['name'], product['id'], (product['quantity'])))
44
45 quantity = 0
46 while True:
47 quantity = safe_input("int_positive", "How many items? ")
48 if quantity > 0 and quantity <= product['quantity']:
49 print("The order is valid. Calculating total price...")
50 break
51 else:
52 print(
53 "The order is invalid. Please choose a number that is not greater than the quantity in stock")
54
55 # we are updating the reference, so this dictionary is also modified
56 # on the products list
57 product['quantity'] -= quantity
58 sale['num_products'] = quantity
59 sale['total_price'] = quantity * product['price']
60
61 print("\nTotal price: $%s (+ $%s tax)" %
62 (sale['total_price'], sale['total_price'] * 0.16))
63
64 sale['id'] = len(get_sales())
65
66 print("\nThis order's id is", sale['id'])
67
68 update_products(products)
69 add_sale(sale)